// 更新文章
$("#updateArticleBtn").on("click", function () {
    var articleId = $(".articleId").val();
    var articleTitle = $(".articleTitle").val();
    var articleParentId = $('.articleCategorySecond option:checked').val();
    var articleState = $('.articleState:checked').val();
    var articleIcon = $(".articleIcon").val();
    var articleContent = testEditor.getMarkdown();
    if (articleTitle != null && articleTitle.length != 0) {
        if (articleParentId != null && articleParentId.length != 0 && articleParentId != 0) {
            if (articleIcon != null && articleIcon.length != 0) {
                if (articleContent != null && articleContent.length != 0) {
                    $.ajax({
                        type: "POST",
                        url: "/articleManage/updateArticle",
                        data: {
                            articleId: articleId,
                            articleTitle: articleTitle,
                            articleParentId: articleParentId,
                            articleState: articleState,
                            articleIcon: articleIcon,
                            articleContent: articleContent
                        },
                        dataType: "json",
                        success: function (data) {
                            if (data.result == "success") {
                                alert("更新成功");
                                window.location.reload();
                            } else{
                                alert("更新失败");
                            }
                        },
                        error: function () {
                            $(".article-add-message").html("服务器错误，发布失败").css({"color": "red"});
                        }
                    });
                } else {
                    $(".article-add-message").html("文章内容不能为空").css({"color": "red"});
                }
            } else {
                $(".article-add-message").html("文章图标不能为空").css({"color": "red"});
            }
        } else {
            $(".article-add-message").html("必须选择文章次分类").css({"color": "red"});
        }
    } else {
        $(".article-add-message").html("文章标题不能为空").css({"color": "red"});
    }
});

// 删除文章
function deleteArticleFun(articleId) {
    $("#deleteArticleBtn").on("click", function () {
        $.ajax({
            type: "GET",
            url: "/articleManage/deleteArticle?articleId=" + articleId,
            dataType: "json",
            success: function (data) {
                if (data.result == "success") {
                    alert("删除成功");
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            },
            error: function () {
                alert("服务器错误，操作失败");
            }
        });
    });
}

// 发布文章
$("#addArticleBtn").on("click", function () {
    var articleTitle = $(".articleTitle").val();
    var articleParentId = $('.articleCategorySecond option:checked').val();
    var articleState = $('.articleState:checked').val();
    var articleIcon = $(".articleIcon").val();
    var articleContent = testEditor.getMarkdown();
    if (articleTitle != null && articleTitle.length != 0) {
        if (articleParentId != null && articleParentId.length != 0 && articleParentId != 0) {
            if (articleIcon != null && articleIcon.length != 0) {
                if (articleContent != null && articleContent.length != 0) {
                    $.ajax({
                        type: "POST",
                        url: "/articleManage/addArticle",
                        data: {
                            articleTitle:articleTitle,
                            articleParentId:articleParentId,
                            articleState: articleState,
                            articleIcon:articleIcon,
                            articleContent:articleContent
                        },
                        dataType: "json",
                        success: function (data) {
                            if (data.result == "success") {
                                alert("发布成功");
                                window.location.reload();
                            }
                        },
                        error: function () {
                            $(".article-add-message").html("服务器错误，发布失败").css({"color": "red"});
                        }
                    });
                } else {
                    $(".article-add-message").html("文章内容不能为空").css({"color": "red"});
                }
            } else {
                $(".article-add-message").html("文章图标不能为空").css({"color": "red"});
            }
        } else {
            $(".article-add-message").html("必须选择文章次分类").css({"color": "red"});
        }
    } else {
        $(".article-add-message").html("文章标题不能为空").css({"color": "red"});
    }
});

// 点击主分类，动态加载子分类
$(".articleCategoryFirst").on("click", function () {
    var articleCategoryParentId = $('.articleCategoryFirst option:checked').val();
    $.ajax({
        type: "POST",
        url: "/articleManage/getArticleCategoryByParentId",
        data: {articleCategoryParentId: articleCategoryParentId},
        dataType: "json",
        success: function (data) {
            $(".articleCategorySecond").empty();
            var son = data.articleCategoryList;
            for (var i = 0; i < son.length; i++) {
                $(".articleCategorySecond").append('<option value="' + son[i].articleCategoryId + '">' + son[i].articleCategoryTitle + '</option>');
            }
        },
        error: function () {
            $(".articleCategorySecond").html("服务器错误，加载文章次分类失败").css({"color": "red"});
        }
    });
});